#!coding=utf-8

import threading
import time

# 创建一个递归锁
rlock = threading.RLock()

def worker():
    # 获取锁
    rlock.acquire()
    print(threading.current_thread().name, "is working...\n")
    time.sleep(1)
    try:
        # 再次获取锁
        rlock.acquire()
        print(threading.current_thread().name, "is working...\n")
        time.sleep(1)
        try:
            # 访问共享数据
            print("i am worker thread\n")
        finally:
            # 第一次释放锁
            print(threading.current_thread().name, "is working...\n")
            rlock.release()
    finally:
        # 第二次释放锁
        print(threading.current_thread().name, "is working...\n")
        rlock.release()
# 创建两个线程
thread1 = threading.Thread(target=worker)
thread2 = threading.Thread(target=worker)

# 启动线程
thread1.start()
thread2.start()

# 等待所有线程结束
thread1.join()
thread2.join()